﻿

#pragma once


//----------------------------------------------------------------
// 线程缓存中的某个线程
//----------------------------------------------------------------
struct FThreadNode
{
    DWORD thread_id;
};


//----------------------------------------------------------------
// 线程缓存池
//----------------------------------------------------------------
extern FAPI DWORD FGetCurrentThreadID();
template < typename _Node >
struct FThreadCache
{
    FLockObject cs;
    _Node node[eCD_MaxMemThread<<1];

    _Node* Get()
    {
        DWORD id = FGetCurrentThreadID();
        for (size_t i = 0; i < (eCD_MaxMemThread<<1); ++i)
        {
            if (!node[i].thread_id)
                break;
            if (node[i].thread_id == id)
                return &node[i];
        }
        return NewNode(id);
    }
    _Node* NewNode(DWORD id)
    {
        cs.Lock();
        for (size_t i = 0; i < (eCD_MaxMemThread<<1); ++i)
        {
            if (!node[i].thread_id)
            {
                node[i].thread_id = id;
                cs.Unlock();
                return &node[i];
            }
        }
        cs.Unlock();
        return 0;
    }
};